<?php
	
	function curPageName()
	{
 		return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
	}

	function getIP()
	{
		return $_SERVER['REMOTE_ADDR'];
	}

	function setSQLDate()
	{
		return date("M-d-Y H:i:s", time());
	}

	/* returns  num rows found */
	function select($table, $field, $data)
	{
		global $db;
		if(!is_int($data)) $data = "'$data'";
		$sql = "SELECT * FROM $table WHERE $field = $data";		
		$query = $db->prepare($sql);
		$query->execute();
		$result = $query->fetchAll();
		return count($result);
	}

	function hr($title, $hr_color='0')
	{
		global $settings;
		lwriteln('`%'.$settings['game']['name'].' - ',false);
		lwriteln('`0'.$title.'`'.$hr_color);
		echo str_repeat("-=", 39).'-<br/>';
	}



	/* LORD COLOR CODER */
	function lwriteln($text, $nl=true)
	{
		$i = 0;
		$c = '';
		$oc = '';											/* INIT OLD COLOR 					*/
		for($i = 0; $i <= strlen($text); $i++)
		{
			
			if(substr($text, $i, 1) == '`')					/* CHECK FOR COLOR TAG 				*/
			{
				$i++;										/* shift past the tag "`" 			*/

				$color = substr($text, $i, 1);				/* what color, or command			*/

				switch($color)
				{
					case '0' : $c = "#55FF40";	break;		/* LT GREEN (NOT BLACK)				*/
					case '1' : $c = "#0000AA";	break;		/* DK BLUE 							*/
					case '2' : $c = "#00AA00";	break;		/* GREEN 							*/
					case '3' : $c = "#00AAAA";	break;		/* CYAN								*/
					case '4' : $c = "#AA0000";	break;		/* RED  							*/
					case '5' : $c = "#AA00AA";	break;		/* PURPLE							*/
					case '6' : $c = "#995500";	break;		/* BROWN							*/
					case '7' : $c = "#AAAAAA";	break;		/* GREY 							*/
					case '8' : $c = "#555555";	break;		/* DK GREY 							*/
					case '9' : $c = "#5555FF";	break;		/* LT BLUE 							*/
					case '!' : $c = "#55FFFF";	break;		/* LT CYAN 							*/
					case '@' : $c = "#00AA00";	break;		/* LT RED 							*/
					case '#' : $c = "#FF55FF";	break;		/* LT PURPLE						*/
					case '$' : $c = "#FFFF55";	break;		/* YELLOW 							*/
					case '%' : $c = "#FFFFFF";	break;		/* WHITE 							*/
					default : die('bad color marker');
				}// end switch			
				$i++;
				
			}// code char found

			if($i == 0) echo "<font color='$c'>";			/* WHILE 1ST(CHAR) SET FIRST font 	*/

			if( ($c != $oc) && ($i != 0))					/* OLD COLR MATCH CURRENT COLOR		*/
			{
				echo "</font><font color='$c'>";			/* CLOSE TAG, OPEN NEW TAG 			*/
			}
			
			$oc = $c;										/* UPDATE 'OLD COLOR'				*/
			echo substr($text, $i, 1);						/* DISPLAY THE CHARACTER 			*/
		}
		if($nl == true) echo '<br/>';						/* CHECK FOR NEW-LINE				*/
	}// end lwriteln();


	/* *********************************************************************************
	 *  CREATES A NEW USER ACCOUNT
	 ***********************************************************************************/
	function createAccount($d)
	{
		global $db;
		$sql = "INSERT INTO account 
		(
			`gmLevel`,`username`,`password`,`email`,`ip`,`signupdate`,`banned`,`bannedReason`
		) VALUES 
		(
			:gmLevel, :username, :password, :email, :ip, :signupdate, :banned, :bannedReason
		)";
		$query = $db->prepare($sql);
		if($query->execute(array
							(
								':gmLevel'=>		0,
								':username'=>		$d['username'],
								':password'=>		$d['password'],
								':email'=>			$d['email'],
								':ip'=>				getIp(),
								':signupdate'=> 	setSQLDate(),
								':banned'=>			0,
								':bannedReason'=>	''
							)
						)
			)
			return true;
		else
			return false;
	}

	/* *********************************************************************************
	 *  CREATES A Character
	 ***********************************************************************************/
	function createCharacter($d)
	{
		global $db;
		$sql = "INSERT INTO characters
		(
			`auid`,`name`,`created`,`laston`,`sex`,`class`
		) VALUES 
		(
			:auid, :name, :created, :laston, :sex, :class
		)";
		$query = $db->prepare($sql);
		if($query->execute(array
							(
								':auid'=>			$_SESSION['id'],
								':name'=>			$d['name'],
								':created'=>		setSQLDate(),
								':laston'=>			setSQLDate(),
								':sex'=>			$d['sex'],
								':class'=> 			$d['class']
							)
						)
			)
			return true;
		else
			return false;
	}
	
	/* *********************************************************************************
	 *  UPDATES CHARACTER
	 ***********************************************************************************/
	function update($id, $field, $new_data)
	{
		global $db;
		$sql = "UPDATE characters SET $field = $new_data WHERE id = $id";
		$db->query($sql);		
	}
	
	function setOnlineStatus($auid, $status)
	{
		global $db;
		$sql = "UPDATE characters SET online = :status WHERE auid = :auid";
		$query = $db->prepare($sql);
		$query->bindParam(':status', $status);
		$query->bindParam(':auid', $auid);
		$query->execute();		
	}
	
	/**
	 * USED FOR WEAPON,ARMOR SHOP LIST
	 * */
	function listFormat($data, $value, $length=35)
	{
		$length_data = strlen($data) - substr_count($data, '`') * 2;
		$length_value = strlen($value) - substr_count($value, ',');
		$final_str = '';
		while ($length_data + $length_value <= $length)
		{
			$data .= '`8.';
			$length_data = strlen($data) - substr_count($data,'`') * 2;
			$length_value = strlen($value) - substr_count($value, ',');
		}
		$final_str = $data.$value;
		return $final_str;
	}

	function setLastOnline($auid)
	{
		global $db;
		$sql = "UPDATE characters SET lastOn = :time WHERE auid = :auid";
		$query = $db->prepare($sql);
		$query->bindParam(':time', date("Y-m-d H:i:s",time()));
		$query->bindParam(':auid', $auid);
		$query->execute();	
	}

	/* @type = 'armor', or 'weapon' */
	function getItemRec($id, $type)
	{
		global $db;
		$sql = "SELECT * FROM $type WHERE id = :id";
		$query = $db->prepare($sql);
		$query->bindParam(':id',$id);
		$query->execute();
		if($query->rowCount() > 0)
			return $query->fetch();
		else
			return null;
	}

	function getCharacterRecord($auid)
	{
		try
		{
			global $db;
			$sql = "SELECT * FROM characters WHERE auid = :auid";
			$query = $db->prepare($sql);
			$query->bindParam(':auid',$auid);
			$query->execute();
			if($query->rowCount() > 0)
				return $query->fetch();
			else
				return null;
		}

		 catch(PDOException $e) {

		echo $e->getMessage().'<br/>';// comment this out on release
		die('Having techical difficulties.Please check back later');

		}
	}

	function getUserRecord($username)
	{
		global $db;
		$sql = "SELECT * FROM account WHERE username = :username";
		$query = $db->prepare($sql);
		$query->bindParam(':username',$username);
		$query->execute();
		if($query->rowCount() > 0)
			return $query->fetch();
		else
			return null;
	}

	function correctLogin($username, $password)
	{
		global $db;
		$sql = "SELECT * FROM account WHERE username = :username AND password = :password";
		$query = $db->prepare($sql);
		$query->bindParam(':username',$username);
		$query->bindParam(':password',$password);
		$query->execute();
		if($query->rowCount() > 0)
			return true;
		else
			return false;
	}
?>